<?php

namespace App\Http\Controllers\Home;

use App\Model\Category;
use App\Model\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends CommonController
{
    /*分类*/
    public function index($id, Request $request)
    {
        //分类信息
        $category = Category::find($id);
        //子分类
        $childcates = Category::where('pid', $id)->get();
        //分类
        $childcatewhere = request('childcate', '');

        //查询数据以及分页
        //如果分类条件是空 则查询所有子分类

        if ($childcatewhere == '') {
            //查询所有子分类
            $childcateids = Category::where('pid', $id)->get()->toArray();

            $childcateidarr = [];
            foreach ($childcateids as $childcateid) {
                $childcateidarr[] = $childcateid['id'];
            }

            $childcateidstr = $childcateidarr;
        } else {
            $childcateidstr[] = $childcatewhere;
        }
        $goods = Goods::whereIn('categories_id', $childcateidstr)->Paginate(5);
        return view('home.category.index', compact('category', 'childcates', 'childcatewhere', 'goods'));

    }
}
